package com.atguigu.springbootdemo.controller;

import com.atguigu.springbootdemo.bean.Employee;
import com.atguigu.springbootdemo.mapper.EmployeeMapper;
import com.atguigu.springbootdemo.service.EmployeeService;
import com.atguigu.springbootdemo.service.EmployeeServiceImpl;
import com.atguigu.springbootdemo.service.EmployeeServiceImpl2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Smexy on 2022/12/5
 */
@RestController
public class EmployeeController
{
    /*
        employeeService= new EmployeeServiceImpl();
               @Autowired: 标注在属性上，代表自动从容器中找一个指定类型的属性，找到就赋值
     */
    @Autowired
    private EmployeeService employeeService;

    /*
        问题一:
            不使用多态，功能上没问题，维护性差！
                举例： v1.0 版本： 根据Id查询员工
                      v2.0 版本:  根据id + 1查询员工。

                      不用多态，service层每个组件的更新，既需要改service还需要改控制器，称为高耦合！
                      使用了多态，低耦合，只需要修改service层

         问题二:
                不分层行不行?
     */
   // private EmployeeServiceImpl employeeService;
    //private EmployeeServiceImpl2 employeeService;

    /*
        /emp? op=insert & id=3 &lastname=ponymama &gender=male &email=tom%40163.com
     */
    @RequestMapping(value = "/emp")
    public Object handle1(String op,String lastname,Integer id,String gender,String email){

        //封装参数为数据模型
        Employee employee = new Employee(id, lastname, gender, email);

        switch (op){
            case "select" : if (id == null){
                return "id非法!";
            }else {
                Employee e = employeeService.getEmployeeById(id);
                return  e == null ? "查无此人" : e;
            }

            case "update": if (id == null){
                return "id非法!";
            }else {
                employeeService.updateEmployee(employee);
                return "更新成功!";
            }

            case "insert": {
                employeeService.insertEmployee(employee);
                return "插入成功!";
            }

            case "delete" : if (id == null){
                return "id非法!";
            }else {
                employeeService.deleteEmployeeById(id);
                return  "删除成功!";
            }

            default: return "成功";

        }

    }

    @Autowired
    private EmployeeMapper dao;

    @RequestMapping(value = "/getAllEmp")
    public Object handle2(){

       /* System.out.println("先做1");

        List<Employee> all = dao.getAll();

        System.out.println("再做2");*/

       return employeeService.getAll();
       // return  all;
    }

    @RequestMapping("/getRegion")
    public Object handle3(){
        return  employeeService.getAllRegion();
    }
}
